import router from "@/router";
import request from "@/services";
import Storage from "@/utils/Storage";
import Vue from "vue";


// 获取用户数据和群聊数据接口
export const getGourpListAPI = (data) => request.post({
    url: '/ThirdMessage/GetGourpList',
    data
})

// 获取个人用户信息接口
export const getUserInfoAPI = (data) => request.post({
    url: '/ThirdMessage/GetUserInfo',
    data
})

// 文件上传的接口
export const UploadImages = (data) => request.post({
    url: '/Common/UploadImagesNoAuth',
    headers: {
        // 需要 multipart/form-data 类型
        'Content-Type': 'multipart/form-data'
    },
    data
})

// 获取用户订单图片列表数据接口
export const getStoreIamgeListAPI = (data) => request.post({
    url: '/ThirdMessage/GetStoreIamgeList',
    data
})

// 是否有确认订单
export const getOrderDateilsAPI = (data) => request.post({
    url: '/ThirdMessage/GetOrderDateils',
    data
})

// 新的获取用户一键修图传的图片接口
export const getPsIamgeListAPI = (data) => request.post({
    url: '/ThirdMessage/GetPsIamgeList',
    data
})

class RestApi {
    constructor() {
        this.users = []
        this.groups = []
    }

    // //用户数据示例
    // users = [
    //     {
    //         id: '08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a',
    //         name: 'Mattie',
    //         password: '123',
    //         avatar: '/static/images/Avatar-1.png',
    //         email: 'Mattie@goeasy.io',
    //         phone: '138xxxxxxxx',
    //     },
    // ];

    // //群数据示例
    // groups = [
    //     {
    //         id: 'group-a42b-47b2-bb1e-15e0f5f9a19a',
    //         name: '小程序交流群',
    //         avatar: '/static/images/wx.png',
    //         userList: [
    //             '08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a',
    //             '3bb179af-bcc5-4fe0-9dac-c05688484649',
    //             'fdee46b0-4b01-4590-bdba-6586d7617f95',
    //             '33c3693b-dbb0-4bc9-99c6-fa77b9eb763f',
    //         ],
    //     },
    // ];

    // 订单数据示例
    // orders = [
    //     {
    //         id: '252364104325',
    //         url: '/static/images/goods1-1.jpg',
    //         name: '青桔柠檬气泡美式',
    //         price: '￥23',
    //         count: 1
    //     },
    // ];

    async fetchHomeFn(params) { // 进入首页执行的函数方法
        await this.fetchGetUserInfoAPI(params)
        await this.fetchGetGourpListAPI(params)

        const { id, groupId, type } = params
        let targetUrl = '/conversations'
        if (id) {
			var currentGroup = this.findGroupById(groupId);
			console.log(currentGroup)
            // 只是进入群聊页面
            if (groupId) {
                const avatar = this.groups.find(item => item.id === groupId)?.avatar ? this.groups.find(item => item.id === groupId)?.avatar : '/static/images/wx.png'
                targetUrl = `/conversations/groupchat/${groupId}?name=${currentGroup.name}&newname=${currentGroup.newname}&avatar=${avatar}`
            }
            // 进入群聊 并且执行某些 type类型的操作
            if (groupId && type) {
                const itemId = params?.itemId ? params.itemId : 0
                const itemItemId = params?.itemItemId ? params.itemItemId : 0

                const avatar = this.groups.find(item => item.id === groupId)?.avatar ? this.groups.find(item => item.id === groupId)?.avatar : '/static/images/wx.png'
                targetUrl = `/conversations/groupchat/${groupId}?name=${currentGroup.name}&newname=${currentGroup.newname}&avatar=${avatar}&type=${type}&itemId=${itemId}&itemItemId=${itemItemId}`
            }
            // 跳转页面并且替换上一个页面的路由
            router.replace({ path: targetUrl })
        }
    }

    async fetchGetGourpListAPI(params) { // 获取用户信息和群组信息
        const { code, data } = await getGourpListAPI(params)
        if (code == 0) {
            this.users = data.users;
            this.groups = data.groups;
        }
    }

    async fetchGetUserInfoAPI(params) { // 获取用户信息
        const { code, data } = await getUserInfoAPI(params)
        if (code == 0) {
            const user = data
            Vue.prototype.globalData.currentUser = user
            Storage.set("currentUser", JSON.stringify(user))
        }
    }

    findUsers() {
        return this.users;
    };

    findFriends(user) {
        return this.users.filter((v) => v.id !== user.id);
    }

    findGroups(user) {
        return this.groups.filter((v) => v.userList.find((id) => id === user.id));
    }

    findUser(username, password) {
        return this.users.find((user) => user.name === username && user.password === password);
    }

    getOrderList() {
        return this.orders;
    }

    findGroupById(groupId) {
        return this.groups.find((group) => group.id === groupId);
    }

    findUserById(userId) {
        return this.users.find((user) => user.id === userId);
    }

    findGroupMembers(groupId) {
        let members = [];
        let group = this.groups.find(v => v.id === groupId);
        this.users.map(user => {
            let userId = group.userList.find((id) => {
                return id === user.id;
            });
            members.push(user);
        });
        return members;
    }

    findGroupMemberAvatars(groupId) {
        let avatars = [];
        let group = this.groups.find((v) => v.id === groupId);
        this.users.map((user) => {
            group.userList.forEach((userId) => {
                if (user.id === userId) {
                    avatars.push(user.avatar);
                }
            });
        });
        return avatars;
    }
}

export default new RestApi();




